package progetto3.managerAB;

import java.util.Random;

public class MyRunnable implements Runnable {

	private Manager manager;
	private String m;
	private int maxCS;
	private int minCS;
	private int min;
	private int max;
	private final int id;
	private Random random = new Random();
	

	public MyRunnable (String m, Manager manager) {
		this.manager = manager;
		this.m=m;
		if(m.equals("A")){
			id=0;
			maxCS=8;
			minCS=2;
			min=0;
			max=30;
		}else{
			id=1;
			maxCS=5;
			minCS=3;
			min=0;
			max=50;
		}
	}
	
	public int getId(){
		return id;
	}

	private void delay(int sleep){
		try {
			Thread.sleep(sleep);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void run() {
		while (!Thread.currentThread().isInterrupted()) {
			manager.request(this);
			System.out.println(m);
			delay((int)random.nextDouble()*(maxCS-minCS)+minCS);
			manager.release(this);
			delay((int)random.nextDouble()*(max-min)+min);
		}
	}//run
}//MyRunnable
